<template>
	<view>
		<view class="cu-bar bg-gradual-green">
			<view class="action">
				<text class="cuIcon-homefill text-white"></text> 首页
			</view>
			<view class="content text-bold">
				盘点系统
			</view>
			<view class="action">
				<text class="cuIcon-scan text-white" v-on:click="showScan"></text>
			</view>
		</view>
		<form>
			<view class="cu-form-group margin-top">
				<view class="title">项目编号:</view>
				<input placeholder="项目编号" name="projectNo" class="text-right" v-model="postData.projectNo" disabled></input>
				<text class='cuIcon-right text-gray'></text>
			</view>
			<view class="cu-form-group">
				<view class="title">分布场所:</view>
				<input placeholder="分布场所" name="projectPlace" class="text-right" v-model="postData.projectPlace" disabled></input>
				<text class='cuIcon-locationfill text-gray'></text>
			</view>
			<view class="cu-form-group">
				<view class="title">库位:</view>
				<input placeholder="库位" name="storeNo" class="text-right" v-model="postData.storeNo" disabled></input>
				<text class='cuIcon-tag text-gray'></text>
			</view>
			<view class="cu-form-group">
				<view class="title">数量:</view>
				<input placeholder="数量" name="number" class="text-right" v-model="postData.number"></input>
				<text class='cuIcon-sort text-gray'></text>
			</view>
			<view style="margin-top: 50rpx;">
				<button class="cu-btn block bg-green margin-tb-sm lg" v-on:click="saveData" :disabled="loading">
					<text class="cuIcon-loading2 cuIconfont-spin" v-show="loading"></text> 提交
				</button>
			</view>
		</form>
		<view class="cu-bar tabbar bg-white foot">
			<view class="action text-green">
				<view class="cuIcon-homefill"></view> 首页
			</view>
			<view class="action text-gray">
				<view class="cuIcon-apps"></view> 项目
			</view>
			<view class="action text-gray add-action">
				<button class="cu-btn cuIcon-add bg-green shadow"></button>
				发布
			</view>
			<view class="action text-gray">
				<view class="cuIcon-calendar">
					<view class="cu-tag badge">99</view>
				</view>
				待办
			</view>
			<view class="action text-gray">
				<view class="cuIcon-my">
					<view class="cu-tag badge"></view>
				</view>
				我的
			</view>
		</view>
	</view>
</template>

<script>
	import * as dd from 'dingtalk-jsapi';
	export default {
		data() {
			return {
				loading: false,
				hasScanData: false,
				postData: {
					projectNo: '',
					projectPlace: '',
					storeNo: '',
					number: 0
				}
			};
		},
		mounted() {
			
		},
		methods: {
			// 显示扫一扫
			showScan: function() {
				let me = this;
				dd.ready(function(){
					dd.biz.util.scan({
					    type: 'all',
					    onSuccess: function(data) {
							let dataArr = data.text.split('*')
							if (dataArr && dataArr.length < 4) {
								uni.showModal({
									content: '数据格式错误',
									showCancel: false
								})
								return
							}
							me.postData.projectNo = dataArr[0]
							me.postData.projectPlace = dataArr[1]
							me.postData.storeNo = dataArr[2]
							me.postData.number = Number(dataArr[3])
							me.hasScanData = true
					    },
					   onFail : function(err) {
						   uni.showToast({ title:"扫码失败" })
						   me.hasScanData = false
					   }
					})
				})
			},
			// 保存
			saveData: function() {
				let me = this
				if (!me.hasScanData) {
					uni.showModal({
						content: '请先点击右上角的扫一扫按钮，扫描目标数据',
						showCancel: false
					})
					return
				}
				me.loading = true
				uni.request({
					url:'http://scanapi.jucheap.com/api/store/save',
					method: 'POST',
					data: me.postData,
					contentType: 'application/json',
					success: (res) => {
						me.loading = false
						if(res.data && res.data !== ''){
							me.hasScanData = false
							me.postData.projectNo = ''
							me.postData.projectPlace = ''
							me.postData.storeNo = ''
							me.postData.number = 0
							uni.showToast({
								title: '提交成功'
							})
						} else {
							uni.showToast({
								title: '提交失败: ' + res.data
							})
						}
					},
					fail: (err) => {
						me.loading = false
						uni.showToast({
							title: '接口请求失败'
						})
					}
				})
			}
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>
